package com.hgx.su.common.springboot.web.advice;

import com.hgx.su.common.pure.constants.GeneralConstant;
import com.hgx.su.common.pure.dto.NullClassDto;
import com.hgx.su.common.pure.dto.ResultResponse;
import com.hgx.su.common.pure.exception.AppException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Description:RestControllerAdvice
 * author: HGX
 * DateTime: 2020-12-21 11:43
 */
@RestControllerAdvice
public class CommonExceptionHandlerAdvice {


    protected static final Logger log = LoggerFactory.getLogger(CommonExceptionHandlerAdvice.class);

    @ExceptionHandler(value = AppException.class)
    public ResultResponse<NullClassDto> appExceptionHandler(AppException e) {
        log.info("全局异常处理,module:{}，moduleName:{},code:{},description:{},AppException:", e.getModule(), e.getModuleName(), e.getCode(), e.getDescription(), e);
        return ResultResponse.error(e.getModule() + e.getCode(), e.getDescription(), e.getModule(), e.getCode());
    }

    @ExceptionHandler(value = Exception.class)
    public ResultResponse<NullClassDto> exceptionHandler(Exception e) {
        log.error("全局异常处理,Exception:", e);
        return ResultResponse.error(GeneralConstant.ERROR_CODE, GeneralConstant.ERROR_DESC);
    }
}
